//
//  AddSideView.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/18.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "AddSideView.h"
#import "IQKeyboardManager.h"

@interface AddSideView()<UITextFieldDelegate>{
    
    CGFloat keyBoardHeight;
}

@end

@implementation AddSideView

-(id)initWithFrame:(CGRect)frame{
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.4];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"添加体侧内容";
        titleLabel.font = BoldBigFont;
        [whiteView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn setImage:@"pop_icon_close"];
        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
        [whiteView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        
        UITextField *nameTF = [[UITextField alloc] init];
        nameTF.backgroundColor = BackGroundColor;
        nameTF.delegate = self;
        nameTF.layer.cornerRadius = 2;
        nameTF.layer.masksToBounds = YES;
        UILabel *nameTFLeft = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 60)];
        nameTFLeft.text = @"名称:";
        nameTFLeft.textAlignment = NSTextAlignmentCenter;
        nameTFLeft.textColor = TFColor;
        nameTFLeft.font = MidBigFont;
        nameTF.leftView = nameTFLeft;
        nameTF.font = MidBigFont;
        //titleTF.backgroundColor = CSRedColor;
        nameTF.leftViewMode = UITextFieldViewModeAlways;
        nameTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        [whiteView addSubview:nameTF];
        self.nameTF = nameTF;
        
        UITextField *unitTF = [[UITextField alloc] init];
        unitTF.delegate = self;
        unitTF.backgroundColor = BackGroundColor;
        unitTF.layer.cornerRadius = 2;
        unitTF.layer.masksToBounds = YES;
        UILabel *unitTFLeft = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 60)];
        unitTFLeft.text = @"描述:";
        unitTFLeft.textAlignment = NSTextAlignmentCenter;
        unitTFLeft.textColor = TFColor;
        unitTFLeft.font = MidBigFont;
        unitTF.leftView = unitTFLeft;
        unitTF.font = MidBigFont;        
        //titleTF.backgroundColor = CSRedColor;
        unitTF.leftViewMode = UITextFieldViewModeAlways;
        unitTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        [whiteView addSubview:unitTF];
        self.unitTF = unitTF;
        
        MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
        [whiteView addSubview:bottomView];
        self.bottomView = bottomView;
        
        __weak __typeof(self) weakSelf = self;
        
        self.bottomView.cancelBlock = ^ {
            
            if (weakSelf.cancelBlock) {
                
                [weakSelf hiddenWithAnimation];
                
                weakSelf.cancelBlock();
            }
        };
        
        self.bottomView.commitBlock = ^ {
            
            if (weakSelf.commitBlock) {
                
                if ([weakSelf.nameTF.text isEmptyString]) {
                    [MBProgressHUD showText:@"请输入名称"];
                    return ;
                }
                
                if (([weakSelf.nameTF.text isEmptyString] || [weakSelf.unitTF.text isEmptyString]) && weakSelf.unitTF.hidden == NO ) {
                    
                    [MBProgressHUD showText:@"请输入名称和单位"];
                    
                    return ;
                }
                
                if ([weakSelf.dict objectForKey:weakSelf.nameTF.text]) {
                    
                    [MBProgressHUD showText:@"已经存在相同的名称"];
                    
                    return ;
                }
                
                weakSelf.commitBlock(weakSelf.nameTF.text,weakSelf.unitTF.text);
                
                [weakSelf hiddenWithAnimation];
                
            }
        };
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(20);
        make.centerX.mas_equalTo(self.whiteView);
    }];
    
    [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.whiteView).offset(-10);
        make.top.mas_equalTo(self.whiteView).offset(10);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.width.mas_equalTo(self.whiteView);
        make.height.mas_equalTo(1);
    }];
    
    [self.nameTF mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(20);
        
        make.left.mas_equalTo(self.whiteView).offset(20);
        
        make.right.mas_equalTo(self.whiteView).offset(-20);
        
        make.height.mas_equalTo(50);
    }];
    
    [self.unitTF mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.nameTF.mas_bottom).offset(10);
        
        make.left.mas_equalTo(self.whiteView).offset(20);
        
        make.right.mas_equalTo(self.whiteView).offset(-20);
        
        make.height.mas_equalTo(50);
    }];
    
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.unitTF.mas_bottom).offset(30);
        
        make.left.width.bottom.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.whiteView);
    }];
}



- (void)showWithAnimation {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    //控制整个功能是否启用。
    manager.enable = NO;
    
    [self addViews];
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    CGFloat height = self.whiteView.frame.size.height;
    self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    //控制整个功能是否启用。
    manager.enable = YES;
    
    [self removeFromSuperview];
}

#pragma mark 键盘处理
-(void)onKeyboardDidShow:(NSNotification *)notice{
    
    CGRect keyBoard = [notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    keyBoardHeight = keyBoard.size.height;
    
    CGFloat orignY = self.height - self.whiteView.bottom;
    
    if (orignY < keyBoardHeight) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self.whiteView.transform = CGAffineTransformMakeTranslation(0, -(keyBoardHeight - orignY));
        }];
    }
}

-(void)onKeyboardDidHide:(NSNotification *)notice{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.whiteView.transform = CGAffineTransformIdentity;
    }];
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
